package org.jsola.hr.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.jsola.common.DateKit;
import org.jsola.contract.dto.TemplateContentDTO;
import org.jsola.contract.entity.ContractDO;
import org.jsola.contract.service.IContractPathService;
import org.jsola.contract.service.ITemplateContentService;
import org.jsola.core.entity.BaseDO;
import org.jsola.exception.ParamException;
import org.jsola.hr.dto.ContractEmpInfoDTO;
import org.jsola.hr.entity.EmpInfoDO;
import org.jsola.hr.service.IEmpInfoService;
import org.jsola.hr.vo.EmpInfoContractVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author zhenghua
 */

@Service("hrTemplateContentServiceImpl")
@Slf4j
public class TemplateContentServiceImpl implements ITemplateContentService {

    @Autowired
    private IEmpInfoService empInfoService;

    @Override
    public List<TemplateContentDTO> replaceTemplateContent(String content, String companyId, Long templateId
            , List<String> phoneList, TokenUser tokenUser) {
        ParamException.hasLength(content, "合同内容不能为空");

        List<EmpInfoDO> empInfoDOList = empInfoService.selectByPhonesAndSiteId(phoneList, tokenUser.getSiteId());
        if (CollectionUtils.isEmpty(empInfoDOList)) {
            return new ArrayList<>();
        }
        ContractEmpInfoDTO contractEmpInfoDTO = new ContractEmpInfoDTO();
        contractEmpInfoDTO.setCompanyId(companyId);
        contractEmpInfoDTO.setTemplateId(templateId);
        contractEmpInfoDTO.setEmpIdList(empInfoDOList.parallelStream().map(BaseDO::getId).collect(Collectors.toList()));
        List<EmpInfoContractVO> empContractInfoList = empInfoService.getEmpContractInfoList(contractEmpInfoDTO, tokenUser.getSiteId());
        if (CollectionUtils.isEmpty(empContractInfoList)) {
            return new ArrayList<>();
        }
        return empContractInfoList.parallelStream().filter(distinctByKey(EmpInfoContractVO::getPhone)).map(empInfoContractVO -> {
            String myContent = content;
            log.info("修改前合同内容：{}", content);
            List<Map<String, Object>> conVariableMapList = empInfoContractVO.getConVariableMap();
            for (Map<String, Object> stringObjectMap : conVariableMapList) {
                for (String key : stringObjectMap.keySet()) {
                    Map<String, Object> map = (HashMap) stringObjectMap.get(key);
                    Object name = map.get("name");
                    Object value = map.get("content");
                    if (name == null) {
                        continue;
                    }
                    if (value == null) {
                        // 值为空时替换为同等数量的空格
                        value = "";
                        for (int i = 0; i < name.toString().length() + 2; i++) {
                            value += " ";
                        }
                    }
                    myContent = myContent.replace("#" + name + "#", value + "");
                }

            }
            TemplateContentDTO templateContentDTO = new TemplateContentDTO();
            templateContentDTO.setPhone(empInfoContractVO.getPhone());
            templateContentDTO.setTemplateContent(myContent);
            return templateContentDTO;
        }).collect(Collectors.toList());
    }


    static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

}
